<template>
    <div>
        <!--      搜索  创建-->
        <el-form :inline="true" :model="form" class="demo-form-inline">
            <el-form-item>
                <el-input clearable v-model="form.applicant" style="width: 300px" placeholder="标题"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="query">搜索</el-button>
            </el-form-item>
        </el-form>
        <!--           列表-->
        <el-table :data="tableData" border>
            <el-table-column
                    fixed
                    prop="theme"
                    label="活动主题"
                    align="center">
            </el-table-column>
            <el-table-column
                    fixed
                    prop="address"
                    label="活动地点"
                    align="center">
            </el-table-column>
            <!-- <el-table-column
                    :formatter="formatDate"
                    prop="start_time"
                    label="开始时间"
                    align="center">
            </el-table-column>
            <el-table-column
                    :formatter="formatDate1"
                    prop="end_time"
                    label="结束时间"
                    align="center">
            </el-table-column> -->
            <el-table-column
                    fixed="right"
                    label="操作"
                    align="center">
                <template slot-scope="scope">
                    <el-button
                            type="success"
                            size="mini"
                            @click="selpeople(scope.row)">报名人
                    </el-button>
            <!--        <el-button
                            type="success"
                            size="mini"
                            @click="handleEdit(scope.row)">编辑
                    </el-button>-->
                    <el-button
                            type="danger"
                            size="mini"
                            @click="handleDelete(scope.row)" >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
<!--        分页-->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNo"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next"
                :total="totalRecord" class="text-center mt10">
        </el-pagination>


<!--        修改弹出框-->
        <el-dialog title="修改" :visible.sync="dialogFormVisibleEdit">
            <el-form :model="ruleFormEdit" :rules="rulesEdit" ref="ruleForm" label-width="180px" class="demo-ruleForm">
                <!--<el-form-item label="报名人" prop="applicant">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.applicant" readonly></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动主题" prop="theme">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.theme" readonly></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动地点" prop="theme">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.address" readonly></el-input>
                    </div>
                </el-form-item>-->
                <el-form-item label="开始时间" prop="start_time">
                    <div style="width: 320px">
                        <!-- <el-input v-model="ruleFormEdit.time"></el-input> -->
                        <el-date-picker
                            style="width: 320px"
                            v-model="ruleFormEdit.start_time"
                            type="date"
                            placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </el-form-item>
                <el-form-item label="结束时间" prop="end_time">
                    <div style="width: 320px">
                        <!-- <el-input v-model="ruleFormEdit.time"></el-input> -->
                        <el-date-picker
                            style="width: 320px"
                            v-model="ruleFormEdit.end_time"
                            type="date"
                            placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
                <el-button type="primary" @click="yes">确 定</el-button>
            </div>
        </el-dialog>
        <!--报名人弹出列表-->
        <el-dialog title="报名人列表" :visible.sync="peopleVisible" width="95%">
            <el-table :data="peopleData" border>
                <el-table-column
                        prop="NAME"
                        label="姓名"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="age"
                        label="年龄"
                        width="80px"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="sex"
                        label="性别"
                        width="80px"
                        align="center">
                    <template slot-scope = "scope">
                        <span v-if="scope.row.sex == '0'">男</span>
                        <span v-if="scope.row.sex == '1'">女</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="address"
                        label="家庭住址"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="nation"
                        label="民族"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="tel"
                        label="联系电话"
                        width="140px"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="idcard"
                        label="身份证号"
                        width="180px"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="start_time"
                        label="开始时间"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="end_time"
                        label="结束时间"
                        align="center">
                </el-table-column>
                <el-table-column
                        label="操作"
                        width="200px"
                        align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="success"
                                size="mini"
                                @click="handleEdit(scope.row)">分配时间
                        </el-button>
                        <el-button
                                type="danger"
                                size="mini"
                                @click="Delete(scope.row)" >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>

    </div>
</template>

<script>


    export default {

        name: 'test',

        data() {
            return {
                themeid:'',
                peopleVisible:false,//报名人弹出框
                peopleData:[],//报名人列表
                tableData: [],
                pageNo: 1,
                pageSize: 10,
                totalRecord: 0,
                totalPage: 0,
                dialogFormVisible: false, //创建弹出框
                dialogFormVisibleEdit:false, //修改弹出框
                form:{},  //创建按钮的表单
                //创建表单
                ruleForm: {
                    title: '',
                    content: '',
                },
                //修改表单
                ruleFormEdit:{},
                //验证
                rules: {
                    title: [
                        { required: true, message: '请输入标题', trigger: 'blur' }
                    ],
                    content: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                },
                //修改验证
                rulesEdit: {
                    title: [
                        { required: true, message: '请输入标题', trigger: 'blur' }
                    ],
                    content: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                },
                formLabelWidth: '120px',
                cid:''
            }
        },

        props: {},

        components: {},

        computed: {},

        methods: {
            // 初始化
            async init() {
                const res = await this.$http('selectactlist',{
                    pageNo: this.pageNo,
                    pageSize: this.pageSize
                })
                // let aaa =res.results
                // console.log(aaa)
                // for (var i = 0; i < aaa.length; i++) {
                //     if (aaa[i].start_time == undefined) {
                //             console.log('start_time',aaa[i].start_time)
                //             aaa[i].start_time == ''
                //             console.log('start_time',aaa[i].start_time)
                //     }
                //     if (aaa[i].end_time == undefined) {
                //             console.log('end_time',aaa[i].end_time)
                //             aaa[i].end_time == ''
                //             console.log('end_time',aaa[i].end_time)
                //     }
                // }
                this.tableData = res.results;
                this.totalPage = res.totalPage;
                this.totalRecord = res.totalRecord;
                console.log(res)
            },
            // //时间转换
            //  formatDate(value){
            //     // <!--例如后台传给我们的时间戳  -->
            //       this.value1= new Date(value.start_time);//value.createdTime是prop绑定的字段名称
            //       let dateValue = this.$moment(this.value1).format("YYYY-MM-DD");//$moment专门转化时间的插件（使用时需要下载引入）
            //       return dateValue
            // },
            handleClick(row) {
                console.log(row);
            },
            //分页
            async handleSizeChange(val) {
                this.pageSize = val;
                this.init();
            },
            async handleCurrentChange(val) {
                this.pageNo = val;
                this.init();
            },
            //创建
            create() {
                this.dialogFormVisible = true
                this.ruleForm.title = ""
                this.ruleForm.content = ""
            },

            //编辑
            handleEdit(row){
                this.dialogFormVisibleEdit = true
                this.cid = row.id
                this.ruleFormEdit = row
            },
            //点击确定即可完成编辑
            async yes(){
              this.dialogFormVisibleEdit =false
                var timestamp = this.ruleFormEdit.start_time;
                console.log('birth',timestamp)
                var d = new Date(timestamp);    //根据时间戳生成的时间对象
                var date = (d.getFullYear()) + "-" +
                    (d.getMonth() + 1) + "-" +
                    (d.getDate())
                this.ruleFormEdit.start_time = date

                var timestamp1 = this.ruleFormEdit.end_time;
                console.log('birth',timestamp1)
                var d1 = new Date(timestamp1);    //根据时间戳生成的时间对象
                var date1 = (d1.getFullYear()) + "-" +
                    (d1.getMonth() + 1)
                this.ruleFormEdit.end_time = date1
                const res = await this.$http('createenlist',{
                    id:this.cid,
                    start_time:this.ruleFormEdit.start_time,
                    end_time:this.ruleFormEdit.end_time
                })
                if (res.count === 1) {
                    this.$message({
                        message: '操作成功',
                        type: 'success'
                    });
                    this.dialogFormVisibleEdit = false
                    this.init()
                } else {
                    this.$message({
                        message: '操作失败',
                        type: 'error'
                    });
                }
            },
            //删除
            async handleDelete(row){
                this.$confirm('将永久删除该内容, 是否继续?', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const res =await this.$http('createactivity',{
                        id:row.id,
                        del_flag:'1'
                    })
                    this.init()
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            async Delete(row){
                this.$confirm('将永久删除该内容, 是否继续?', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const res =await this.$http('createenlist',{
                        id:row.id,
                        del_flag:'1'
                    })
                    const res1 = await this.$http('selecenlistlist',{
                        theme_id:this.theme_id,
                    });
                    this.peopleData = res1;
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //搜索
            async query(){
                const res = await this.$http('selectapplicantlist',{
                    title:this.form.title
                });
                this.tableData = res.results;
                this.totalPage = res.totalPage;
                this.totalRecord = res.totalRecord;
            },
            async selpeople(row){
                this.themeid = row.id
                const res = await this.$http('selecenlistlist',{
                    theme_id:row.id,
                });
                this.peopleData = res;
                console.log('111',res);
                this.peopleVisible = true
            },
        },

        watch: {},

        mounted() {
            this.init();
        },

    }
</script>

<style scoped>

</style>
